// var vConsole = new VConsole();
var map;
var lu;
var driving;

var indexMap = 0;
var mapPoints = [];
var defaultPoints = {
  lng: 116.30447,
  lat: 40.058563,
  plateNo: "京FQ8426",
  appearTime: "",
};
var defaultPointsIndex = false; //true 则清空mapPoints里的默认坐标
function question(dateTime) {
  $.ajax({
    // url: 'http://testapp1.xiaotongkids.com/xiaotong-ai-customer-web-test-1/register/checkcode',
    url:
      "http://202.85.223.75:8090/zpark/StructureMotorVehicleController/queryStructureMotorVehicleByTimeAndPlateNO", //测试
    type: "get",
    data: {
      curTime: 20200723080700, //20200723092000,//20200723080700,  //20200723080700,
      interval: 500,
      plateNO: "京FQ8426", //'京A01721D'//'京FQ8426'
    },
    dataType: "JSON",
    // contentType: false,
    // processData: false,
    success: function (res) {
      if (res.code === 200) {
        // console.log(new.Data())latitude,longitude
        res.Data.data = res.Data.data || [];
        // 如果有默认坐标则清空数组
        if (defaultPointsIndex) {
          mapPoints = [];
        }
        res.Data.data.forEach((item) => {
          mapPoints.push({
            lng: item.longitude,
            lat: item.latitude,
            plateNo: item.plateNo,
            appearTime: item.appearTime,
          });
        });

        // 如果没有坐标则显示默认坐标
        if (mapPoints.length === 0) {
          mapPoints = [defaultPoints];
          defaultPointsIndex = true;
        }

        // 初始化地图
        if (indexMap === 0) {
          init();
        }
        // new BMap.Point(mapPoints) = res.Data.data;
      } else {
      }
    },
  });
}
question();
function updateDriving(id) {
  driving.search(
    new BMap.Point(mapPoints[id].lng, mapPoints[id].lat),
    new BMap.Point(
      mapPoints[id + 1] ? mapPoints[id + 1].lng : mapPoints[id].lng,
      mapPoints[id + 1] ? mapPoints[id + 1].lat : mapPoints[id].lat
    )
  );
  driving.setSearchCompleteCallback(function () {
    points = driving.getResults().getPlan(0).getRoute(0).getPath();
    if (indexMap === 0) {
      lu = new Dls({
        icon: false,
        marker: false,
        map: map,
        points: points,
        timeStr: 10,
        end: function (res) {
          // 减2 indexMap不能是最后一个 则需要减2
          if (indexMap < mapPoints.length - 2) {
            indexMap = indexMap + 1;
            updateDriving(indexMap);
          }
        },
      });
      lu.show();
    } else {
      lu.updatedPoints({
        points: points,
        timeStr: 10,
      });
    }
    lu.play();
  });
}

function init() {
  map = new BMap.Map("map");
  map.enableScrollWheelZoom();
  map.centerAndZoom(new BMap.Point(mapPoints[0].lng, mapPoints[0].lat), 20);
  driving = new BMap.DrivingRoute(map);
  updateDriving(indexMap);
}
